package com.sls.mall.order.service.impl;

import com.rabbitmq.client.Channel;
import com.sls.mall.order.entity.OrderEntity;
import com.sls.mall.order.entity.OrderReturnReasonEntity;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sls.mall.common.utils.PageUtils;
import com.sls.mall.common.utils.Query;

import com.sls.mall.order.dao.OrderItemDao;
import com.sls.mall.order.entity.OrderItemEntity;
import com.sls.mall.order.service.OrderItemService;


@RabbitListener(queues = {"ceshi-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    @SneakyThrows
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity reasonEntity, Channel channel){

        // Channel 内按顺序自增 1 2 3 ....
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {

            // 消息头属性值
            MessageProperties messageProperties = message.getMessageProperties();


//        System.out.println(Thread.currentThread().getName()+" :接收到的消息内容："+message);
            System.out.println(Thread.currentThread().getName()+" 接收到的消息内容并转换为实体："+reasonEntity);

            Thread.sleep(5000);

           int a = 1/0;
            // 手动 ACK  false 为非批量
            channel.basicAck(deliveryTag,false);

        }catch (Exception e)
        {
            //void basicNack(
            // long var1,       channel 的 DeliveryTag
            // boolean var3,    是否批量
            // boolean var4)    是否重新入队  false 为丢弃消息 进入死信路由
            channel.basicNack(deliveryTag,false,false);

        }


    }

    @RabbitHandler
    public void recieveMessage(OrderEntity orderEntity){

        System.out.println(" 接收到的消息内容并转换为实体："+orderEntity);

    }

}